// components/user-add/index.js
import {
  request
} from '../../api/index'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    checked: false,
    form: {
      name: '',
      loginName: '',
      password: ''
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClose(update = false) {
      this.triggerEvent('onClose', update);
    },
    onSubmit(event) {
      const values = event.detail.value;
      const valid = Object.keys(values).every(it => !!values[it]);
      if (!valid) {
        wx.showToast({
          title: '存在未填项目',
          icon: 'error'
        })
        return
      }
      const data = Object.assign({}, {
        root: this.data.checked
      }, values)
      request.userAdd(data, () => {
        this.onClose(true);
        this.onReset();
      })
    },
    onChange(event) {
      const value = event.detail;
      this.setData({
        checked: value
      })
    },
    onReset() {
      this.setData({
        checked: false,
        form: {
          name: '',
          loginName: '',
          password: ''
        }
      })
    }
  }
})